package sz.szsmk.citizencard.ui.adapter;

import android.app.Application;
import android.content.Context;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.HomeHotViewBinding;
import sz.szsmk.citizencard.model.network.bean.response.QueryPageResourceRes;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/6/12 10:47
 * description:
 */
public class HomeHotAdapter extends DelegateAdapter.Adapter<HomeHotAdapter.HomeHotViewHolder> {
    private Context mContext;
    private DefaultLayoutHelper mDefaultLayoutHelper;
    private QueryPageResourceRes viewData;
    private ViewPager hotVp;
    private ArrayList<String> imageList;
    //    private HotPageAdapter hotPageAdapter;
    private ImageView hotIv;
    private View view;
    private ArrayList<View> viewList;

    public HomeHotAdapter(Application ins, DefaultLayoutHelper hotSaleHelper) {
        mContext = ins;
        mDefaultLayoutHelper = hotSaleHelper;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mDefaultLayoutHelper;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.home_hot_view;
    }


    @Override
    public int getItemCount() {
        return 1;
    }

    @Override
    public HomeHotViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return HomeHotViewHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(HomeHotViewHolder holder, int position) {
        loadItemData(position, holder.binding);
    }

    private void loadItemData(int position, HomeHotViewBinding binding) {
        ViewPager userCardVp = binding.userCardVp;
        List<View> viewList = new ArrayList<View>();
//        View view = LayoutInflater.from(mContext).inflate(R.layout.user_vp_view, null, false);
        View view1 = LayoutInflater.from(mContext).inflate(R.layout.user_vp_view, null, false);
        View view2 = LayoutInflater.from(mContext).inflate(R.layout.user_vp_view, null, false);
        View view3 = LayoutInflater.from(mContext).inflate(R.layout.user_vp_view, null, false);
//        if (viewData != null) {
//            List<QueryPageResourceRes.ResourceListBean> resource_list = viewData.getResource_list();
//            ArrayList<View> mViewList = new ArrayList<>();
//            for (int i = 0; i < resource_list.size(); i++) {
//                mViewList.add(view1);
//
//            }
//
//        }
        viewList.add(view1);
//        viewList.add(view);
        viewList.add(view2);
        viewList.add(view3);
        MyPagerAdapter vpAdapter = new MyPagerAdapter(viewList);

        userCardVp.setAdapter(vpAdapter);
        userCardVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        if (viewData != null) {
//         binding.
//            hotIv = (ImageView) binding.getRoot().findViewById(R.id.hot_vp_iv);
//            hotVp = binding.hotVp;
            List<QueryPageResourceRes.ResourceListBean> resource_list = viewData.getResource_list();
            imageList = new ArrayList<>();
            viewList = new ArrayList<>();
            for (int i = 0; i < resource_list.size(); i++) {
                String link_url = resource_list.get(i).getResource_url();
                view = LayoutInflater.from(mContext).inflate(R.layout.hot_vp_item, null, false);
                viewList.add(view);

                imageList.add(link_url);
            }

//            hotPageAdapter = new HotPageAdapter(DaggerApplication.getIns(), imageList, hotIv);
//            hotVp.setAdapter(hotPageAdapter);

//            new LoadingPageAdapter(this, imgList, dataBinding.startHomeIv);
        }

        RecyclerView homeHotRv = binding.homeHotRv;
        HomeHotItemAdapter homeHotItemAdapter = new HomeHotItemAdapter();
        GridLayoutManager layoutManager = new GridLayoutManager(DaggerApplication.getIns(), 2);
        homeHotRv.setLayoutManager(layoutManager);
        homeHotRv.setAdapter(homeHotItemAdapter);
    }

    public void setViewData(QueryPageResourceRes viewData) {
        this.viewData = viewData;
    }

    static class HomeHotViewHolder extends RecyclerView.ViewHolder {
        private HomeHotViewBinding binding;

        public static HomeHotViewHolder creat(ViewGroup parent) {
            HomeHotViewBinding binding = HomeHotViewBinding
                    .inflate(LayoutInflater.from((parent.getContext())), parent, false);
            return new HomeHotViewHolder(binding);
        }

        public HomeHotViewHolder(HomeHotViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;

        }
    }

    private class MyPagerAdapter extends PagerAdapter {

        private List<View> mViewList;

        MyPagerAdapter(List<View> viewList) {
            mViewList = viewList;
        }

        @Override
        public int getCount() {
            return mViewList.size();
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = mViewList.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViewList.get(position));
        }

    }

}
